home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Night Owl 6
/
Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso
/
036a
/
pmfinder.zip
/
SCAN.C
< prev
next >
Wrap
Text File
|
1991-12-05
|
8KB
|
277 lines
/*
* OS/2 PM File Finder Utility - Thread SOURCE CODE
*
* LANGUAGE : Microsoft C6.0
* MODEL : large - MT
* ENVIRONMENT : IBM OS/2 PM Toolkit v1.3
* STATUS : operational
*
* This module contains the code for the worker threads
* that do the searching
*/
#define INCL_DOSPROCESS
#define INCL_WINWINDOWMGR
#define INCL_WINLISTBOXES
#define INCL_WINMESSAGEMGR
#define NUL '\0'
#include <os2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>
#include "scan.h"
/* global variables */
extern BOOL fContinue;
extern HSEM hsemWait;
/**/
void cdecl FAR ScanThread( PSCANTHREADPARM pstp)
{
CHAR temp_str[2]; /* temporary spec string */
CHAR szFileSpec[64]; /* file specification string */
CHAR szArchivePattern[12]; /* pattern to search archives */
CHAR szArchive[4]; /* archive indicator */
int i;
static HMQ hmqScan; /* handle to message queue */
static HAB habScan; /* handle to anchor block */
fContinue= TRUE;
/* This is a real cheap way to get multithreading. You should
not use a message queue if you don't post messages to it.
You should write the data to a global circular buffer and then
post a message that data is ready to be written to the listbox.
I was just too lazy to do so.
See Stephen Best's OS/2 Threads Cookbook for an implementation
of such. Available on BIX and OS/2 Shareware BBS as COOKBO.ZIP */
/* initialize thread & create message queue */
habScan = WinInitialize( 0 );
hmqScan = WinCreateMsgQueue( habScan, 0 );
szArchivePattern[0] = NUL;
strcpy(szArchive,pstp->szArchive);
/* clear the semaphore, the next thread can use the structure*/
DosSemClear(hsemWait);
/* for all the drive do the search */
for (i=0;pstp->drive_spec[i] && i < 27 && fContinue;i++)
{
/* create directory listing using pattern */
temp_str[0] = pstp->drive_spec[i];
temp_str[1] = NUL;
strcpy(szFileSpec,temp_str);
strcat(szFileSpec,":\\");
/* if this is an archive thread then put the pattern
in szArchivePattern and search for *.szArchive */
if (strlen(szArchive))
{
strcpy(szArchivePattern,strupr(pstp->szPattern));
strcat(szFileSpec,"*.");
strcat(szFileSpec,strupr(szArchive));
}
else
strcat(szFileSpec,pstp->szPattern);
fContinue = Directory(
szFileSpec,
FILE_NORMAL,
pstp->hwndListBox,
szArchivePattern
);
}
/* Issue thread done message */
WinSendMsg(
pstp->hwndDlg,
WM_THREAD_DONE,
NUL,
NUL
);
WinDestroyMsgQueue( hmqScan );
WinTerminate( habScan );
_endthread();
}
/*
* Directory( szFileSpec, fsAttributes, hwndListBox, szArchivePattern ) : BOOL;
*
* szFileSpec file search specification
* fsAttributes file attributes to search for
* hwndListBox handle to listbox for directory
* szArchivePattern the archive type to search
*
* This function searches the disk for files of the specified type
* and appends the results found to the list box whose handle is
* provided. An error is generated and the process aborted if
* insufficient memory is available for the search.
* It also will search the specified archive for files matching the
* pattern
*
* A value of TRUE is returned if the directory operation was
* successful.
*
*/
static BOOL Directory(
PSZ szFileSpec,
USHORT fsAttributes,
HWND hwndListBox,
PSZ szArchivePattern )
{
/* RM - Some of the array sizes may be to small to work with
the HPFS long file names - */
BOOL fDirOutput; /* boolean directory flag */
USHORT iChar; /* index into char string */
CHAR szPath[260]; /* path specification string */
CHAR szSpec[64]; /* search specification string */
HDIR hdirSearch; /* handle to search directory */
USHORT usSearchCount; /* # of files to search for */
FILEFINDBUF findbuf; /* search result buffer */
CHAR szEntry[260]; /* current file name */
CHAR szFullname[260]; /* full file name and path */
SHORT sErr; /* error code */
/* initialization */
fContinue = TRUE;
fDirOutput = FALSE;
/* Give up timeslice for the other threads */
DosSleep(1);
/* separate file spec into path and wildcards */
for ( iChar=strlen(szFileSpec)-1; szFileSpec[iChar]!='\\'; iChar-- );
strcpy( szPath, szFileSpec );
szPath[iChar] = '\0';
strcpy( szSpec, &szFileSpec[iChar+1] );
/* perform search for normal files */
DosFindClose(hdirSearch);
hdirSearch = HDIR_CREATE;
usSearchCount = 1;
sErr = DosFindFirst(
szFileSpec,
&hdirSearch,
fsAttributes,
&findbuf,
sizeof(findbuf),
&usSearchCount,
0L
);
if ( !sErr ) {
/* repeat until all entries exhausted */
do {
if ( fContinue ) {
/* If this thread is to search archives, call procedure */
if (strlen(szArchivePattern))
{
strcpy(szFullname,szPath);
strcat(szFullname,"\\");
strcat(szFullname,findbuf.achName);
/* A better way to do this, is to use function
pointers and the next three ifs would be just
one call. */
if( strstr(szFileSpec,".ZIP") )
DoZip(szArchivePattern,szFullname,hwndListBox);
if( strstr(szFileSpec,".LZH") )
DoLzh(szArchivePattern,szFullname,hwndListBox);
if( strstr(szFileSpec,".ARC") )
DoArc(szArchivePattern,szFullname,hwndListBox);
if( strstr(szFileSpec,".PAK") )
DoArc(szArchivePattern,szFullname,hwndListBox);
}
else
{
/* output current path and file name */
sprintf(
szEntry,
"%s%s%s",
szPath,
"\\",
findbuf.achName
);
/* add entry to file list box */
if ( fContinue )
AddToListBox(hwndListBox,szEntry);
}
}
/* get next entry */
if ( fContinue )
sErr = DosFindNext(
hdirSearch,
&findbuf,
sizeof(findbuf),
&usSearchCount
);
} while ( fContinue && !sErr );
}
if ( fContinue ) {
/* perform search for sub-directories */
sprintf( szEntry, "%s\\*.*", szPath );
DosFindClose(hdirSearch);
hdirSearch = HDIR_CREATE;
usSearchCount = 1;
sErr = DosFindFirst(
szEntry,
&hdirSearch,
FILE_DIRECTORY,
&findbuf,
sizeof(findbuf),
&usSearchCount,
0L
);
if ( !sErr ) {
/* repeat until all entries exhausted */
do {
/* eliminate special directory entries */
if ( findbuf.attrFile == FILE_DIRECTORY
&& findbuf.achName[0] != '.' ) {
sprintf(
szEntry,
"%s\\%s\\%s",
szPath,
findbuf.achName,
szSpec
);
fContinue = Directory( szEntry, fsAttributes, hwndListBox,
szArchivePattern);
}
/* get next entry */
if ( fContinue )
sErr = DosFindNext(
hdirSearch,
&findbuf,
sizeof(findbuf),
&usSearchCount
);
} while ( fContinue && !sErr );
}
}
/* return final result */
return fContinue;
}